<<<<<<< HEAD
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Accountcash_model extends CI_Model {
	
	function __construct(){
        parent::__construct();
        
    }
    
    function arrays($key){
    	$data = array(
    		'account_bank'  => array(0=>"未填写",
						300=>"工商银行",301=>"中国银行",302=>"建设银行",303=>"农业银行",463=>"交通银行",
						465=>"广发银行",466=>"招商银行",467=>"平安银行",468=>"兴业银行",469=>"民生银行",
						470=>"华夏银行",471=>"上海浦东发展银行",472=>"中信银行",473=>"光大银行",493=>"深圳发展银行",
						494=>"桂林商业银行",495=>"湛江商行",496=>"鄞州银行",497=>"进出口银行",498=>"农业发展银行",
						499=>"国家开发银行",500=>"富滇银行",501=>"深圳农村商业银行",502=>"柳州市商业银行",503=>"吉林银行",
						504=>"上海农商行",505=>"西安商行",506=>"北京银行",507=>"上海银行",508=>"浙商银行",
						509=>"东亚银行",510=>"恒生银行",511=>"汇丰中国",512=>"渣打银行",513=>"花旗银行",
						514=>"星展银行",515=>"徽商银行",516=>"南京银行",517=>"恒丰银行",518=>"广州银行",
						519=>"厦门国际银行",520=>"荷兰银行",521=>"华侨银行",522=>"德意志银行",523=>"杭州银行",
						524=>"宁波银行",525=>"渤海银行",526=>"永亨银行中国",527=>"南商银行中国",528=>"友利银行中国",
						529=>"中信嘉华",530=>"巴黎银行中国",531=>"三菱东京日联银行",532=>"瑞穗银行",533=>"北京农商行",
						534=>"邮政储蓄银行",535=>"东莞银行",536=>"汉口银行",537=>"法国兴业银行",538=>"泉州商业银行",
						539=>"成都银行",540=>"厦门市商业银行",541=>"福建海峡银行",542=>"包商银行",543=>"大连银行",
						544=>"哈尔滨银行",545=>"大庆商行",
						),
						/**
				'account_cash_status' 	=> array("0"=>"等待审核", "1"=>"审核通过", 
						"2"=>"审核拒绝", "3"=>"用户取消", "4"=>"到账","5"=>"提现成功", "6"=>"提现失败", "7"=>"转账中"),
						*/
						
'account_cash_status' 	=> array("0"=>"等待审核", "1"=>"审核通过",
			"2"=>"审核拒绝", "3"=>"用户取消", "4"=>"到账"),
						
				'user_loan' 	=> array("0"=>"投资客", "1"=>"借款户"),
    	);
    	
    	if($key && isset($data[$key])){
    		return $data[$key];
    	}else{
    		return array();
    	}
    }
    
    function get_by_id($id){
    	$this->db->where('id', $id);
    	$query = $this->db->get('rsh_account_cash');
    	return $query->row_array();
    }
    
	function update($id, $data){
		$this->db->where('id', $id);
		$this->db->update('rsh_account_cash', $data);
		return $this->db->affected_rows();
	}
    
	function get_accountcash_list($data){
		//封装查询内容
		$_select = "c.id, c.user_id, u.username, u.realname, u.is_loan, c.fstatus, c.account, 
					c.bank, c.branch, c.province, c.city, c.total, c.credited, c.fee, c.add_time, c.verify_time, c.verify_remark";
		
		//封装条件
		$_where = " where 1=1 ";
		if(isset($data['where']['user_id']) && $data['where']['user_id'] != ''){
			$_where .= " and u.user_id = '".$data['where']['user_id']."'";
		}
		if(isset($data['where']['username']) && $data['where']['username'] != ''){
			$_where .= " and u.username like '%".$data['where']['username']."%'";
		}
		if(isset($data['where']['realname']) && $data['where']['realname'] != ''){
			$_where .= " and u.realname like '%".$data['where']['realname']."%'";
		}
		if(isset($data['where']['is_loan']) && $data['where']['is_loan'] != -1){
			$_where .= " and u.is_loan=".$data['where']['is_loan'];
		}
		if(isset($data['where']['fstatus']) && $data['where']['fstatus'] != -1){
			$_where .= " and c.fstatus=".$data['where']['fstatus'];
		}
		if(isset($data['where']['fstatus']) && $data['where']['fstatus'] === '0'){
			if(isset($data['where']['strtime']) && $data['where']['strtime'] != 0){
				$strtime = strtotime($data['where']['strtime'].' 00:00:00');
				$_where .= " and c.add_time >= ".$strtime;
			}
			if(isset($data['where']['endtime']) && $data['where']['endtime'] != 0){
				$endtime = strtotime($data['where']['endtime'].' 23:59:59');
				$_where .= " and c.add_time <= ".$endtime;
			}
		}else{
			if(isset($data['where']['strtime']) && $data['where']['strtime'] != 0){
				$strtime = strtotime($data['where']['strtime'].' 00:00:00');
				$_where .= " and c.verify_time >= ".$strtime;
			}
			if(isset($data['where']['endtime']) && $data['where']['endtime'] != 0){
				$endtime = strtotime($data['where']['endtime'].' 23:59:59');
				$_where .= " and c.verify_time <= ".$endtime;
			}
		}
		
		$_order = " order by id desc ";
		$_limit = " limit ".$data['page_start'].",".$data['page_size'];
		//封装sql
		$sql_result = "select $_select from rsh_account_cash as c 
						left join rsh_user as u on c.user_id=u.user_id";
		$sql_result .= $_where;
		$sql_result .= $_order;
		if(isset($data['limit']) && $data['limit'] == 'all'){
			$query = $this->db->query($sql_result);
			$data['accountcash_list'] = $query->result_array();
			return $data;
		}else{
			$sql_result .= $_limit;	
			$query = $this->db->query($sql_result);
			$data['accountcash_list'] = $query->result_array();
		}
		
		//总记录条数
		$sql_count = "select count(*) as count from rsh_account_cash as c 
						left join rsh_user as u on c.user_id=u.user_id
						";
		$sql_count .= $_where;
		$query = $this->db->query($sql_count);
		$data['accountcash_count'] = $query->row()->count;
		
		//总金额
		$sql_count = "select sum(total) as total from rsh_account_cash as c 
						left join rsh_user as u on c.user_id=u.user_id
						";
		$sql_count .= $_where;
		$query = $this->db->query($sql_count);
		$data['accountcash_total'] = $query->row()->total;
		
		return $data;
	}
	
	/**
	 * 获取到账的提现总额
	 */
	function get_succ_total($user_id){
		$sql = "select sum(total) as total,sum(credited) as credited,sum(fee) as fee from rsh_account_cash where user_id='{$user_id}' and fstatus=4";
		$query = $this->db->query($sql);
		return $query->row_array();
	}
	
	function add($data){
		$this->db->insert('rsh_account_cash', $data);
		return $this->db->insert_id();
	}
	
	/**
	 * 费用扣除
	 */
	function deduct($data = array()){
		$admin_user_id = $data['admin_user_id'];
		$user_id = $data['user_id'];
		$account = $data['account'];
		$remark  = $data['remark'];
		$ftype   = $data['ftype'];
		$ip		 = $data['ip'];
		
		$this->load->model('Account_model');
		$user_account = $this->Account_model->get_account($user_id);
		$log['user_id'] 		= $user_id;
		$log['ftype'] 			= $ftype;
		$log['money'] 			= $account;
		$log['total'] 			= $user_account['total']-$log['money'];
		$log['use_money'] 		= $user_account['use_money']-$log['money'];
		$log['no_use_money'] 	= $user_account['no_use_money'];
		$log['collection'] 		= $user_account['collection'];
		$log['to_user'] 		= $admin_user_id;
		$log['remark'] 			= $remark;
		$log['add_time'] 		= time();
		$log['addip'] 			= $ip;
		$mode = substr(strval($user_id),-1,1);
		/*tangc 死锁问题*/
		$this->db->trans_strict(FALSE);
    	$this->db->trans_begin();
		$this->db->insert('rsh_account_log_'.$mode, $log);
		$this->db->query("update rsh_account set total=total-{$log['money']},use_money=use_money-{$log['money']} where user_id={$user_id};");
		if ($this->db->trans_status() === TRUE) {
	        $this->db->trans_commit();
	        return 1;
	    } else {
	        $this->db->trans_rollback();
	        //@todo 异常处理部分
	        return 0;
	    }
	}
	
	//****** by liub 20141111 提现汇总
	function get_accountcash_statistics($data){
		$_select  = " c.add_time as time,
			SUM(case when c.fstatus = 4 then c.credited else 0 end)  as total_money_over,
			count(case when c.fstatus =4 then c.id else NULL end)   as total_items_over,
			SUM(case when (c.fstatus = 1 or c.fstatus = 0 ) then c.credited else 0 end)   as total_money_in,
			count(case when (c.fstatus = 1 or c.fstatus = 0 )then c.id else NULL end)   as total_items_in,
			SUM(case when (c.fstatus = 1 or c.fstatus = 0) then c.credited else 0 end)/count(case when (c.fstatus = 1 or c.fstatus = 0)then c.id else NULL end)   as avg_money_all,
			max(case when (c.fstatus = 1 or c.fstatus = 0 ) then c.credited else 0 end) as max_money,
			min(case when (c.fstatus = 1 or c.fstatus = 0 ) then c.credited else 0 end ) as min_money";
		
		$_where = " where 1=1 ";
		
		// 时间戳范围
		if(isset($data['where']['strtime']) && $data['where']['strtime'] != 0){
			$strtime = strtotime($data['where']['strtime'].' 00:00:00');
			$_where .= " and c.add_time >= ".$strtime;
		}
		if(isset($data['where']['endtime']) && $data['where']['endtime'] != 0){
			$endtime = strtotime($data['where']['endtime'].' 23:59:59');
			$_where .= " and c.add_time <= ".$endtime;
		}
		
		$_group = " group by  DATE_FORMAT(FROM_UNIXTIME(c.add_time),'%Y%m%d') ";
		$_order = " order by c.id desc ";
		$_limit = " limit ".$data['page_start'].",".$data['page_size'];
		
		//封装sql
		$sql_result = "select $_select from rsh_account_cash c LEFT JOIN rsh_user AS u ON c.user_id=u.user_id ";
		$_where .= " and u.is_self = 0 and ( c.fstatus = 0 or c.fstatus = 4 or c.fstatus = 1)";
		$sql_result .= $_where;
		$sql_result .= $_group;
		$sql_result .= $_order;
		
		//echo $sql_result;
		//exit();
		
		if(isset($data['limit']) && $data['limit'] == 'all'){
			$query = $this->db->query($sql_result);
			$data['accountcash_list'] = $query->result_array();
			return $data;
		}else{
			$sql_result .= $_limit;
			$query = $this->db->query($sql_result);
			$data['accountcash_list'] = $query->result_array();
		}
		
		//总记录条数
		$sql_count = " select count(*) as count from (select 1  from rsh_account_cash c LEFT JOIN rsh_user AS u ON c.user_id=u.user_id $_where $_group) as vt";
		$query = $this->db->query($sql_count);
		$data['accountcash_count'] = $query->row()->count;
		
		return $data;
	}
	
	
	
	// $fstatus = 4
	function get_user_cash_total($user_id,$fstatus){
		$sql = "select sum(total) as total from rsh_account_cash where user_id = {$user_id} and fstatus = {$fstatus} ";
		$query = $this->db->query($sql);
		//echo $sql;exit;
		$data = $query->row()->total;
		return $data;
		
	}
	
	
=======
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Accountcash_model extends CI_Model {
    
	function __construct(){
        parent::__construct();
        
    }
    
    function arrays($key){
    	$data = array(
    		'account_bank'  => array(0=>"未填写",
						300=>"工商银行",301=>"中国银行",302=>"建设银行",303=>"农业银行",463=>"交通银行",
						465=>"广发银行",466=>"招商银行",467=>"平安银行",468=>"兴业银行",469=>"民生银行",
						470=>"华夏银行",471=>"上海浦东发展银行",472=>"中信银行",473=>"光大银行",493=>"深圳发展银行",
						494=>"桂林商业银行",495=>"湛江商行",496=>"鄞州银行",497=>"进出口银行",498=>"农业发展银行",
						499=>"国家开发银行",500=>"富滇银行",501=>"深圳农村商业银行",502=>"柳州市商业银行",503=>"吉林银行",
						504=>"上海农商行",505=>"西安商行",506=>"北京银行",507=>"上海银行",508=>"浙商银行",
						509=>"东亚银行",510=>"恒生银行",511=>"汇丰中国",512=>"渣打银行",513=>"花旗银行",
						514=>"星展银行",515=>"徽商银行",516=>"南京银行",517=>"恒丰银行",518=>"广州银行",
						519=>"厦门国际银行",520=>"荷兰银行",521=>"华侨银行",522=>"德意志银行",523=>"杭州银行",
						524=>"宁波银行",525=>"渤海银行",526=>"永亨银行中国",527=>"南商银行中国",528=>"友利银行中国",
						529=>"中信嘉华",530=>"巴黎银行中国",531=>"三菱东京日联银行",532=>"瑞穗银行",533=>"北京农商行",
						534=>"邮政储蓄银行",535=>"东莞银行",536=>"汉口银行",537=>"法国兴业银行",538=>"泉州商业银行",
						539=>"成都银行",540=>"厦门市商业银行",541=>"福建海峡银行",542=>"包商银行",543=>"大连银行",
						544=>"哈尔滨银行",545=>"大庆商行",
						),
						/**
				'account_cash_status' 	=> array("0"=>"等待审核", "1"=>"审核通过", 
						"2"=>"审核拒绝", "3"=>"用户取消", "4"=>"到账","5"=>"提现成功", "6"=>"提现失败", "7"=>"转账中"),
						*/
						
'account_cash_status' 	=> array("0"=>"等待审核", "1"=>"审核通过",
			"2"=>"审核拒绝", "3"=>"用户取消", "4"=>"到账"),
						
				'user_loan' 	=> array("0"=>"投资客", "1"=>"借款户"),
    	);
    	
    	if($key && isset($data[$key])){
    		return $data[$key];
    	}else{
    		return array();
    	}
    }
    
    function get_by_id($id){
    	$this->db->where('id', $id);
    	$query = $this->db->get('rsh_account_cash');
    	return $query->row_array();
    }
    
	function update($id, $data){
		$this->db->where('id', $id);
		$this->db->update('rsh_account_cash', $data);
		return $this->db->affected_rows();
	}
    
	function get_accountcash_list($data){
		//封装查询内容
		$_select = "c.id, c.user_id, u.username, u.realname, u.is_loan, c.fstatus, c.account, 
					c.bank, c.branch, c.province, c.city, c.total, c.credited, c.fee, c.add_time, c.verify_time, c.verify_remark";
		
		//封装条件
		$_where = " where 1=1 ";
		if(isset($data['where']['user_id']) && $data['where']['user_id'] != ''){
			$_where .= " and u.user_id = '".$data['where']['user_id']."'";
		}
		if(isset($data['where']['username']) && $data['where']['username'] != ''){
			$_where .= " and u.username like '%".$data['where']['username']."%'";
		}
		if(isset($data['where']['realname']) && $data['where']['realname'] != ''){
			$_where .= " and u.realname like '%".$data['where']['realname']."%'";
		}
		if(isset($data['where']['is_loan']) && $data['where']['is_loan'] != -1){
			$_where .= " and u.is_loan=".$data['where']['is_loan'];
		}
		if(isset($data['where']['fstatus']) && $data['where']['fstatus'] != -1){
			$_where .= " and c.fstatus=".$data['where']['fstatus'];
		}
		if(isset($data['where']['fstatus']) && $data['where']['fstatus'] === '0'){
			if(isset($data['where']['strtime']) && $data['where']['strtime'] != 0){
				$strtime = strtotime($data['where']['strtime'].' 00:00:00');
				$_where .= " and c.add_time >= ".$strtime;
			}
			if(isset($data['where']['endtime']) && $data['where']['endtime'] != 0){
				$endtime = strtotime($data['where']['endtime'].' 23:59:59');
				$_where .= " and c.add_time <= ".$endtime;
			}
		}else{
			if(isset($data['where']['strtime']) && $data['where']['strtime'] != 0){
				$strtime = strtotime($data['where']['strtime'].' 00:00:00');
				$_where .= " and c.verify_time >= ".$strtime;
			}
			if(isset($data['where']['endtime']) && $data['where']['endtime'] != 0){
				$endtime = strtotime($data['where']['endtime'].' 23:59:59');
				$_where .= " and c.verify_time <= ".$endtime;
			}
		}
		
		$_order = " order by id desc ";
		$_limit = " limit ".$data['page_start'].",".$data['page_size'];
		//封装sql
		$sql_result = "select $_select from rsh_account_cash as c 
						left join rsh_user as u on c.user_id=u.user_id";
		$sql_result .= $_where;
		$sql_result .= $_order;
		if(isset($data['limit']) && $data['limit'] == 'all'){
			$query = $this->db->query($sql_result);
			$data['accountcash_list'] = $query->result_array();
			return $data;
		}else{
			$sql_result .= $_limit;	
			$query = $this->db->query($sql_result);
			$data['accountcash_list'] = $query->result_array();
		}
		
		//总记录条数
		$sql_count = "select count(*) as count from rsh_account_cash as c 
						left join rsh_user as u on c.user_id=u.user_id
						";
		$sql_count .= $_where;
		$query = $this->db->query($sql_count);
		$data['accountcash_count'] = $query->row()->count;
		
		//总金额
		$sql_count = "select sum(total) as total from rsh_account_cash as c 
						left join rsh_user as u on c.user_id=u.user_id
						";
		$sql_count .= $_where;
		$query = $this->db->query($sql_count);
		$data['accountcash_total'] = $query->row()->total;
		
		return $data;
	}
	
	/**
	 * 获取到账的提现总额
	 */
	function get_succ_total($user_id){
		$sql = "select sum(total) as total,sum(credited) as credited,sum(fee) as fee from rsh_account_cash where user_id='{$user_id}' and fstatus=4";
		$query = $this->db->query($sql);
		return $query->row_array();
	}
	
	function add($data){
		$this->db->insert('rsh_account_cash', $data);
		return $this->db->insert_id();
	}
	
	/**
	 * 费用扣除
	 */
	function deduct($data = array()){
		$admin_user_id = $data['admin_user_id'];
		$user_id = $data['user_id'];
		$account = $data['account'];
		$remark  = $data['remark'];
		$ftype   = $data['ftype'];
		$ip		 = $data['ip'];
		
		$this->load->model('Account_model');
		$user_account = $this->Account_model->get_account($user_id);
		$log['user_id'] 		= $user_id;
		$log['ftype'] 			= $ftype;
		$log['money'] 			= $account;
		$log['total'] 			= $user_account['total']-$log['money'];
		$log['use_money'] 		= $user_account['use_money']-$log['money'];
		$log['no_use_money'] 	= $user_account['no_use_money'];
		$log['collection'] 		= $user_account['collection'];
		$log['to_user'] 		= $admin_user_id;
		$log['remark'] 			= $remark;
		$log['add_time'] 		= time();
		$log['addip'] 			= $ip;
		$mode = substr(strval($user_id),-1,1);
		/*tangc 死锁问题*/
		$this->db->trans_strict(FALSE);
    	$this->db->trans_begin();
		$this->db->insert('rsh_account_log_'.$mode, $log);
		$this->db->query("update rsh_account set total=total-{$log['money']},use_money=use_money-{$log['money']} where user_id={$user_id};");
		if ($this->db->trans_status() === TRUE) {
	        $this->db->trans_commit();
	        return 1;
	    } else {
	        $this->db->trans_rollback();
	        //@todo 异常处理部分
	        return 0;
	    }
	}
	
	//****** by liub 20141111 提现汇总
	function get_accountcash_statistics($data){
		$_select  = " c.add_time as time,
			SUM(case when c.fstatus = 4 then c.credited else 0 end)  as total_money_over,
			count(case when c.fstatus =4 then c.id else NULL end)   as total_items_over,
			SUM(case when (c.fstatus = 1 or c.fstatus = 0 ) then c.credited else 0 end)   as total_money_in,
			count(case when (c.fstatus = 1 or c.fstatus = 0 )then c.id else NULL end)   as total_items_in,
			SUM(case when (c.fstatus = 1 or c.fstatus = 0) then c.credited else 0 end)/count(case when (c.fstatus = 1 or c.fstatus = 0)then c.id else NULL end)   as avg_money_all,
			max(case when (c.fstatus = 1 or c.fstatus = 0 ) then c.credited else 0 end) as max_money,
			min(case when (c.fstatus = 1 or c.fstatus = 0 ) then c.credited else 0 end ) as min_money";
		
		$_where = " where 1=1 ";
		
		// 时间戳范围
		if(isset($data['where']['strtime']) && $data['where']['strtime'] != 0){
			$strtime = strtotime($data['where']['strtime'].' 00:00:00');
			$_where .= " and c.add_time >= ".$strtime;
		}
		if(isset($data['where']['endtime']) && $data['where']['endtime'] != 0){
			$endtime = strtotime($data['where']['endtime'].' 23:59:59');
			$_where .= " and c.add_time <= ".$endtime;
		}
		
		$_group = " group by  DATE_FORMAT(FROM_UNIXTIME(c.add_time),'%Y%m%d') ";
		$_order = " order by c.id desc ";
		$_limit = " limit ".$data['page_start'].",".$data['page_size'];
		
		//封装sql
		$sql_result = "select $_select from rsh_account_cash c LEFT JOIN rsh_user AS u ON c.user_id=u.user_id ";
		$_where .= " and u.is_self = 0 and ( c.fstatus = 0 or c.fstatus = 4 or c.fstatus = 1)";
		$sql_result .= $_where;
		$sql_result .= $_group;
		$sql_result .= $_order;
		
		//echo $sql_result;
		//exit();
		
		if(isset($data['limit']) && $data['limit'] == 'all'){
			$query = $this->db->query($sql_result);
			$data['accountcash_list'] = $query->result_array();
			return $data;
		}else{
			$sql_result .= $_limit;
			$query = $this->db->query($sql_result);
			$data['accountcash_list'] = $query->result_array();
		}
		
		//总记录条数
		$sql_count = " select count(*) as count from (select 1  from rsh_account_cash c LEFT JOIN rsh_user AS u ON c.user_id=u.user_id $_where $_group) as vt";
		$query = $this->db->query($sql_count);
		$data['accountcash_count'] = $query->row()->count;
		
		return $data;
	}
	
	
	
	// $fstatus = 4
	function get_user_cash_total($user_id,$fstatus){
		$sql = "select sum(total) as total from rsh_account_cash where user_id = {$user_id} and fstatus = {$fstatus} ";
		$query = $this->db->query($sql);
		//echo $sql;exit;
		$data = $query->row()->total;
		return $data;
		
	}
	
	
>>>>>>> d47623b610d79a4eeb489500830c00d70933f414
}